package com.szmirren.vxApi.core.common.mq;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

/**
 * @ClassName : RocketMQConsumer
 * @Description :
 * @Author : 孙亮
 * @Date: 2020-11-19 16:27
 */
public class RocketMQConsumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMQConfig.producerGroup);
        consumer.setNamesrvAddr(RocketMQConfig.host);
        consumer.setInstanceName(RocketMQConfig.consumerInstanceName);

        consumer.subscribe(RocketMQConfig.Topic, RocketMQConfig.tag);
        consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeConcurrentlyContext) -> {
            for (MessageExt msg : list) {
                System.out.println(msg.getMsgId() + "---" + new String(msg.getBody()));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
        System.out.println("Consumer Started...");

    }
}
